package com.example.syyao.myapp.WebContact;


import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 访问网络工具类
 */
public class WebAccessTools {

    /** * post的方式请求
     *@param username 用户名
     *@param password 密码
     *@return 返回null 登录异常
     */
    //登录
//    private static  String pa = "http://192.168.1.111:8082/";
//    private static  String pa = "http://192.168.31.82:8082/";
//    private static String pa="http://172.20.10.3:8082/";
    private static String pa="http://120.79.205.71:8082/";

    public static String login(String username,String password){

        String path = pa + "login";
        //数据准备
        String data = "username="+username+"&password="+password;
        return post(path,data);
    }

    //获取验证码
    public static String identify(String number){
        System.out.println("number:"+number);
        String path = pa + "identify";
        //数据准备
        String data = "number="+number;
        return post(path,data);
    }

    //注册
    public static String register(String username,String password){
        String path = pa + "register";
        //数据准备
        String data = "username="+username+"&password="+password;
        return post(path,data);
    }
    //获取个人信息
    public static String Get_Personal_Information(String username){
        String path = pa + "Get_Personal_Information";
        //数据准备
        String data = "username="+username;
        return post(path,data);
    }
    //获取关注列表
    public static  String Get_Conner_Lsit(String username){
        String path = pa + "Conner_List";
        String data = "username="+username;
        return post(path,data);
    }
    //取消关注
    public static String canel_Conner(String id,String username){
        String path = pa + "Delete_Conner";
        String data = "id="+id+"&username="+username;
        return post(path,data);
    }
    //获取粉丝列表
    public static String Get_fans_List(String username){
        String path = pa + "Fans_List";
        String data = "username="+username;
        return post(path,data);
    }
    //移除粉丝
    public static String canel_fans(String id,String username){
        String path = pa + "Delete_fans";
        String data = "id="+id+"&username="+username;
        return post(path,data);
    }
    //改变名字
    public static String changename(String username,String nickname){
        String path = pa + "changeNickname";
        String data = "username="+username+"&nickname="+nickname;
        return post(path,data);
    }
    //主页
    public static String indexInformation(){
        String path = pa +"main_pager";
        String data="";
        return post(path,data);
    }
    //个人动态
    public static String Get_Personal_Movement(String username){
        String path = pa +"Get_Personal_Movement";
        String data = "username="+username;
        return post(path,data);
    }
    public static String Get_Personal_Infor(String username){
        String path = pa + "Get_Personal_infor";
        //数据准备
        String data = "username="+username;
        return post(path,data);
    }

    public static String Get_pot_content(String type){
        String path=pa+"Get_Pot_Movement";
        String data="pot_name="+type;
        return post(path,data);
    }

    public static String Get_Personal_Move(String username){
        String path = pa +"Get_Personal_Move";
        String data = "username="+username;
        return post(path,data);
    }

    public static String photo(String[] s1,String[] s2,String username) {
        String path = pa + "Move";
        String data = "";
        for(int i=0;i<s1.length;i++){
            if(i==0){
                data="tag="+s1[i];
            }else {
                data=data+"&tag="+s1[i];
            }

        }
        data=data+"&username="+username;
        for(int i=0;i<s2.length;i++){
            data=data+"&file="+s2[i];
            System.out.println(i);
        }

        return post(path,data);
    }

    public static String headpicture(String pic,String username){
        String path= pa+"head_picture";
        String data="bitmapStr="+pic+"&username="+username;
        return post(path,data);
    }

    public static String post(String path,String data){
        try {
            URL url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();


            connection.setConnectTimeout(5000);
            connection.setRequestMethod("POST");

            //至少要设置的两个请求头
            connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
         //   connection.setRequestProperty("Content-Length", data.length()+"");
            System.out.println(data.length());

            //post的方式提交实际上是留的方式提交给服务器
            connection.setDoOutput(true);
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(data.getBytes());

            //获得结果码
            int responseCode = connection.getResponseCode();
            if(responseCode ==200){
                //请求成功
                InputStream inputStream = connection.getInputStream();
                return IOUtils.toString(inputStream, "utf-8");
            }else {
                //请求失败
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

//    public static String picturePost(String path,String[] s1,String[] s2,String username){
//        try {
//            // 创建json对象
//            JSONObject jsonObject = new JSONObject();
//            // 1个数组参数
//            JSONArray jsonArray = new JSONArray();
//            for (String tag : tags) {
//                jsonArray.put(tag);
//            }
//            jsonObject.put("tags", jsonArray);
//            // 3个字符串参数
//            jsonObject.put("appId", appId);
//            jsonObject.put("token", token);
//            jsonObject.put("clientId", clientId);
//            String data = jsonObject.toString();
//            // 构造请求体
//            RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), data);
//            Request request = new Request.Builder()
//                    .url("https://api.ros.ai/player/cms/modules")
//                    .post(body)
//                    .build();
//            // 向服务器异步请求数据
//            OkHttpClient okHttpClient = new OkHttpClient();
//            okHttpClient.newCall(request).enqueue(new okhttp3.Callback() {
//                @Override
//                public void onFailure(okhttp3.Call call, IOException e) {
//
//                }
//
//                @Override
//                public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
//                    ResponseBody body = response.body();
//
//                }
//            });
//        } catch (JSONException e) {
//            e.printStackTrace();
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//
//    }

}